/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.altius.dao.impl;

import cc.altius.dao.VendorDao;
import cc.altius.model.Vendor;
import cc.altius.model.VendorDetail;
import cc.altius.model.VendorSubtype;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author jitendra
 */
@Repository
public class VendorDaoImpl implements VendorDao {

    @Autowired
    private SessionFactory sessionFactory;
    private Session session;
    private Query query;

    @Override
    @Transactional
    public List<VendorDetail> getVendorDetailList() {
        return this.sessionFactory.getCurrentSession().getNamedQuery("VendorDetail.findAll").list();
    }

    @Override
    @Transactional
    public List<VendorSubtype> getVendorSubtypeList() {
        //session=this.sessionFactory.getCurrentSession();
        //Criteria criteria = session.createCriteria(VendorSubtype.class );
        //return criteria.setProjection(Projections.distinct(Projections.property("vendorType"))).list();
        return this.sessionFactory.getCurrentSession().getNamedQuery("VendorSubtype.findAll").list();
    }

    @Override
    @Transactional
    public int addVendorDetail(VendorDetail vendorDetail) {
        return (Integer) this.sessionFactory.getCurrentSession().save(vendorDetail);
    }

    @Override
    @Transactional
    public int addVendor(Vendor vendor) {
        return (Integer) this.sessionFactory.getCurrentSession().save(vendor);
    }

    @Override
    @Transactional
    public List<Vendor> getVendorList(int centerId, int vendorId) {
        session = this.sessionFactory.getCurrentSession();
        String hqlQuery = "from Vendor v  WHERE 1=1 ";
        if (centerId != 0) {
            hqlQuery += " AND v.healthSpringCenter.centerId=" + centerId + "";
        }

        if (vendorId != 0) {
            hqlQuery += " AND v.vendorDetail.vendorDetailId=" + vendorId + "";
        }
        try {
            query = session.createQuery(hqlQuery);
            return (List<Vendor>) query.list();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    @Transactional
    public Vendor getVendorById(int vendorId) {
        Session session = this.sessionFactory.getCurrentSession();
        Criteria criteria = session.createCriteria(Vendor.class);
        criteria.add(Restrictions.eq("vendorId", vendorId));
        return (Vendor) criteria.uniqueResult();
    }

    @Override
    @Transactional
    public int updateVendor(Vendor vendor) {
        session = this.sessionFactory.getCurrentSession();
        String hql = "Update "
                + " Vendor as p"
                + " set"
                + " p.healthSpringCenter.centerId=:centerId,"
                + " p.vendorDetail.vendorDetailId=:vendorDetailId,"
                + " p.vendorSubtype.vendorSubId=:vendorSubId"
                + " WHERE p.vendorId=:vendorId";

        query = session.createQuery(hql);
        query.setParameter("centerId", vendor.getHealthSpringCenter().getCenterId());
        query.setParameter("vendorDetailId", vendor.getVendorDetail().getVendorDetailId());
        query.setParameter("vendorSubId", vendor.getVendorSubtype().getVendorSubId());
        query.setParameter("vendorId", vendor.getVendorId());
        System.out.println("vendor.getHealthSpringCenter().getCenterId()" + vendor.getHealthSpringCenter().getCenterId());
        System.out.println("vendor.getVendorDetail().getVendorDetailId()" + vendor.getVendorDetail().getVendorDetailId());
        System.out.println("vendor.getVendorSubtype().getVendorSubId()" + vendor.getVendorSubtype().getVendorSubId());
        System.out.println("vendor.getVendorId()" + vendor.getVendorId());
        int result = query.executeUpdate();
        return result;
    }

    @Override
    @Transactional
    public VendorDetail getVendorDetailById(int vendorDetailId) {
        Session session = this.sessionFactory.getCurrentSession();
        Criteria criteria = session.createCriteria(VendorDetail.class);
        criteria.add(Restrictions.eq("vendorDetailId", vendorDetailId));
        return (VendorDetail) criteria.uniqueResult();
    }

    @Override
    @Transactional
    public int updateVendorDetail(VendorDetail vendorDetail) {
        Session session = this.sessionFactory.getCurrentSession();
        String hql = "Update "
                + " VendorDetail as p"
                + " set"
                + " p.vendorName=:vendorName,"
                + " p.contact1Person=:contact1Person,"
                + " p.contact1Number=:contact1Number,"
                + " p.contact2Person=:contact2Person,"
                + " p.contact2Number=:contact2Number,"
                + " p.email=:email,"
                + " p.description=:description,"
                + " p.hs_person_responsible=:hs_person_responsible"
                + " WHERE p.vendorDetailId=:vendorDetailId";

        Query query = session.createQuery(hql);
        query.setParameter("vendorName", vendorDetail.getVendorName());
        query.setParameter("contact1Person", vendorDetail.getContact1Person());
        query.setParameter("contact1Number", vendorDetail.getContact1Number());
        query.setParameter("contact2Person", vendorDetail.getContact2Person());
        query.setParameter("contact2Number", vendorDetail.getContact2Number());
        query.setParameter("email", vendorDetail.getEmail());
        query.setParameter("description", vendorDetail.getDescription());
        query.setParameter("hs_person_responsible", vendorDetail.getHs_person_responsible());
        query.setParameter("vendorDetailId", vendorDetail.getVendorDetailId());

        int result = query.executeUpdate();
        return result;
    }
    
    @Override
    @Transactional
    public List<VendorSubtype> getVendorSubListFromType(String vendortype){
        session = this.sessionFactory.getCurrentSession();        
        return session.createQuery("from VendorSubtype v where v.vendorType='"+vendortype+"'").list();
    }
}
